#include "kernel/astralapi.h"
#include "strapi.h"
#include "shell.h"


void show_identifier()
{
    char currpath[20];
    void get_work_dir(char *);
    get_work_dir(currpath);
    kprint("root@astral:\033[31m%s\033[0m$ ", currpath);
    flush_kstdout();
}

int help(int argc, char* argv[])
{
    extern struct command __start_shell;
    extern struct command __stop_shell;

    for(struct command* v = &__start_shell; v < &__stop_shell; v++)
    {
        kprint("%10s %s\n", v->name, v->help);
    }
    return 0;
}

DEFINE_STATIC_CMD(help, "list commands of shell")

void execute_shell(int argc, char* argv[])
{
    boolean handled = False;
    extern struct command __start_shell;
    extern struct command __stop_shell;

    for(struct command* v = &__start_shell; v < &__stop_shell; v++)
    {
        if(strncmp(argv[0], v->name, 100) == 0)
        {
            v->func(argc, argv);
            handled = True;
            break;
        }
    }
    if(!handled)
    {
        kerror("unknown command %s\n", argv[0]);
    }

}

int shell_entry(void *arg)
{
    char cmd[100];
    int status;
    int init_ramfs();
    status = init_ramfs();
    if(0 == status)
    {
        kprint("mount ram file system done.\n");
    }

    terminal_init();

    int argc = 0;
    char *argv[10];

    while (1)
    {
        show_identifier();

        kscan(cmd);

        construct_param(cmd, 10, &argc, argv);

        if(argc == 0)
        {
            continue;
        }
    
        execute_shell(argc, argv);

    }
    return 0;
}


void exec(int argc, char *argv[]);
DEFINE_STATIC_CMD(exec, "execute an elf");